<template>
	<view>
		<view class="status_bar">
			<image class="status_bar_img" src="../../static/lipdee/fh@2x.png" mode="" @tap="jumpTo()"></image>
			<!-- 这里是状态栏 -->
		</view>
		<!-- 整个页面 -->
		<view class="outer_box">
			<view class="welcome">
				欢迎来到相伴医路会员中心~
			</view>
			<view class="authorization">
				授权微信头像、昵称
			</view>
			<view class="logo">
				<image v-if="avatar==''" src="../../static/lipdee/xiangban.png" mode=""></image>
				<image v-else :src="avatar" mode=""></image>
			</view>
			<view class="info" v-if="active==1">
				<view class="">
					为提供优质服务，相伴会员中心需要获取你的以下信息:
					
				</view>
				<view>
					·你的公开信息(头像、昵称等)
				</view>
			</view>
			<!-- 登录按钮 -->
			<view class="btn-login" v-if="active==1">
				<button  @tap="authUserInfo">授权进入相伴医路会员中心</button>

			</view>
			<view class="btn-login" v-if="active==2">
				<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信用户一键登录</button>
			
			</view>
			<!-- 手机号 -->
			<!-- <view class="phone" v-if="active==2">
				<text>手机号码登录</text>
			</view> -->
			<!-- 用户协议 -->
			<privacy/>

		</view>
		<view class="">
			<w-loading text="加载中.." mask="true" click="true" ref="loading"></w-loading>
		</view>


	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				active: 1,
				code: '',
				sessionKey:'',
				isAuthInfo:true,
				avatar:'',
				sex:'',
				nickName:''
			};
		},
		onLoad() {
			// #ifdef MP-WEIXIN
			uni.login({
				success: (res) => {
					if (res.errMsg == "login:ok") {
						this.code = res.code;
					} else {
						uni.showToast({
							title: '系统异常，请联系管理员!'
						})
					}
				}
			})
			// #endif
		
		},
		methods:{
			...mapMutations(['login', 'logout','setUserInfo']),
			//返回上一页
			jumpTo(){
				uni.reLaunch({
					url:'../index/index'
				})
			},
			// #ifdef MP-WEIXIN
			/* 获取用户信息 */
			authUserInfo(e) {
				let _this = this
				uni.showLoading({
				    title: '加载中'
				});
				uni.getUserProfile({
					desc: '用户信息获取',
					success: res => {
						//存储信息
						let resUserInfo = JSON.parse(res.rawData)
						let provider = {
							userId: '',
							image: resUserInfo.avatarUrl,
							name: resUserInfo.nickName,
							gender: resUserInfo.gender,
							country: resUserInfo.country,
							province: resUserInfo.province,
							city: resUserInfo.city,
							code: _this.code
						}
						_this.sex = resUserInfo.gender
						_this.nickName = resUserInfo.nickName
						_this.avatar = resUserInfo.avatarUrl
						//赋值用户信息
						_this.$api_common.getSessionKey({
							code: _this.code,
							rawData: res.rawData,
							signature: res.signature,
							encryptedData: res.encryptedData,
							iv: res.iv,
							accessId:'W1rM29P3AISsoVkCvx7fOvQer',
						}).then((res) => {
							console.log(res)
							if (res.data.success) {
								//存储用户信息
								provider.userId = res.data.dataMap.userId
								// _this.login(provider)
								_this.sessionKey = res.data.dataMap.sessionKey
								_this.isAuthInfo = !_this.isAuthInfo
								uni.showToast({
									icon: 'none',
									title: '授权成功',
									duration: 1500
								})
								_this.active=2
							} else {
								console.log("接口异常")
							}
							uni.hideLoading();
						
						})
					},
					fail(res) {
						uni.hideLoading();
						console.log('失败了')
					}
				})
			},
			// #endif
			/* 获取用户手机号 */
			getPhoneNumber: function(e) {
				let _this = this
				// console.log(e);
				if (e.detail.errMsg == 'getPhoneNumber:fail:access denied') {
					console.log('用户拒绝提供手机号');
				} else {
					console.log('用户同意提供手机号');
					console.log(JSON.stringify(e.rawData))
					console.log(JSON.stringify(e.detail.encryptedData));
					console.log(JSON.stringify(e.detail.iv));
					var encryptedData = e.detail.encryptedData;
					var iv = e.detail.iv;
					this.$api_common.login({
						type:3,
						sessionKey:_this.sessionKey,
						encryptedData:e.detail.encryptedData,
						iv:e.detail.iv,
						sex:_this.sex,
						image:_this.avatar,
						nickName:_this.nickName
					}).then((res) =>{
						if(res.data.success){
							console.log(res.data.dataMap)
							let userInfo ={
								token:res.data.dataMap.token,
								userInfo:{
									
								}
							}
							_this.login(userInfo)
							_this.getUserInfo()
							// _this.setToken(res.data.dataMap)
							uni.navigateBack({
								delta:1
							})
							uni.$emit('carData',1)
							// console.log(_this.userInfo.token)
						}else{
							console.log("接口异常")
						}
			
					})
				}
			
			},
			// 获取用户信息
			getUserInfo() {
				this.$api_http.getUserInfo().then((res) => {
					let userInfo = res.data.dataMap
					// this.getUserIn = res.data.dataMap
					console.log(userInfo)
					if (userInfo != null) {
						this.setUserInfo(userInfo)
					}
			
				})
			
			},
		}
	}
</script>

<style lang="less" scoped>
	.outer_box {
		padding: 80upx 10upx 0 30upx;
		.phone{
			color: #666666;
			text-align: center;
			margin-top: 30rpx;
		}
		

		.btn-login {
			button {
				width: 92%;
				height: 100upx;
				background: linear-gradient(90deg, #FF8308 0%, #FFBF69 100%);
				border-radius: 50upx;
				color: #FEFEFE;
				font-size: 32upx;
				line-height: 100upx;
			}
		}

		.welcome {
			color: #333333;
			font-size: 44upx;
		}

		.authorization {
			color: #666666;
			font-size: 32upx;
			margin: 20upx 0;
		}

		.logo {
			display: flex;
			justify-content: center;
			margin: 80upx 0;
			image {
				width: 210rpx;
				height: 210rpx;
				border-radius: 50%;
			}

		}

		.info {
			font-size: 24upx;

			view:nth-child(2) {
				color: #999999;
				margin: 20upx 0 50upx;
			}
		}
	}

	.status_bar {
		height: var(--status-bar-height);
		width: 100%;
		.status_bar_img{
			width: 20rpx;
			height: 40rpx;
			// background-color: #F0AD4E;
			margin-top: 70rpx;
			margin-left: 20rpx;
		}
	}
</style>
